package me.xuym.crawler.simpleextension.dispatcher;

import com.cbs.java.component.application.Application;
import me.xuym.crawler.Processor;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by broche on 05/08/2017.
 */
public class SimpleEmailListDispatcher extends Processor<List<SimpleEmailDispatcherInput>, List<SimpleEmailDispatcherInput>> {
    private List<String> emails = new ArrayList<>();
    private String account = "";
    private String password = "";
    private String host = "";
    private String subject = "";

    public SimpleEmailListDispatcher(Application application, List<String> emails, String account, String password, String host, String subject) throws IOException {
        super(application);

        this.emails.addAll(emails);
        this.account = account;
        this.password = password;
        this.host = host;
        this.subject = subject;
    }

    @Override
    protected List<SimpleEmailDispatcherInput> onProcess(List<SimpleEmailDispatcherInput> simpleEmailDispatcherInputs) throws Exception {
        if (emails == null || emails.size() == 0) {
            return simpleEmailDispatcherInputs;
        }
        if (account == null || account.equals("")) {
            return simpleEmailDispatcherInputs;
        }
        if (password == null || password.equals("")) {
            return simpleEmailDispatcherInputs;
        }
        if (host == null || host.equals("")) {
            return simpleEmailDispatcherInputs;
        }
        if (simpleEmailDispatcherInputs == null || simpleEmailDispatcherInputs.size() == 0) {
            return simpleEmailDispatcherInputs;
        }

        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.host", host);
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(account, password);
            }
        };
        Session session = Session.getInstance(properties, authenticator);
        MimeMessage mimeMessage = new MimeMessage(session);
        List<InternetAddress> emaillist = new ArrayList<>();
        for (String email : emails) {
            emaillist.add(new InternetAddress(email));
        }

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<html><body>");
        for (SimpleEmailDispatcherInput input : simpleEmailDispatcherInputs) {
            stringBuilder.append(input.simpleEmailDispatcherContent());
            stringBuilder.append("<br />");
            stringBuilder.append("<br />");
        }
        stringBuilder.append("</body></html>");

        mimeMessage.setFrom(new InternetAddress(account));
        mimeMessage.setRecipients(Message.RecipientType.TO, emaillist.toArray(new Address[]{}));
        mimeMessage.setSubject(subject, "utf-8");
        mimeMessage.setContent(stringBuilder.toString(), "text/html;charset=utf-8");
        Transport.send(mimeMessage);

        return simpleEmailDispatcherInputs;
    }
}
